package com.example.jobback.intercept.common;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.example.jobback.config.AuthConfig;
import com.example.jobback.po.Seeker;
import com.example.jobback.util.CACHEUtil;
import com.example.jobback.util.JWTUtil;
import com.example.jobback.util.SecureEncryptionUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * @ClassName: CommonService
 * @Description: 拦截器通用服务类
 * @Author: lhb
 * @Date: 2025/5/14
 */

@Service
public class CommonInterceptService {


    private final AuthConfig authConfig;

    private final CACHEUtil cacheUtil;

    public CommonInterceptService(AuthConfig authConfig, CACHEUtil cacheUtil) {
        this.authConfig = authConfig;
        this.cacheUtil = cacheUtil;
    }


    /**
     * @Description: 拦截器通用验证token方法
     * @param token
     * @param response
     * @param jwtSecret
     * @param keyPrefix
     * @param userType
     * @return T
     * @Author lhb
     * @CreateTime 2025/5/29 22:04
     */
    public <T> T  checkToken(String token, HttpServletResponse response, String jwtSecret, String keyPrefix,Class<T> userType) throws Exception {
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        if (token == null || token.isEmpty()) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getWriter().write("Token 缺失");
            return null;
        }
        String userIdentity = JWTUtil.checkToken(token, response, jwtSecret);
        if (StrUtil.isBlank(userIdentity)) return null;
        String userIdStr = SecureEncryptionUtil.decrypt(userIdentity,authConfig.getJwtIdSecret());
        //通过redis查看用户是否已登录
        String key=keyPrefix+userIdStr;
        Map<Object,Object> userMap=cacheUtil.getHash(key);
        String redisToken = (String) userMap.get("token");
        // 从 Redis 获取用户 JSON 数据
        String userJson = (String) userMap.get("user");
        // 使用 userType 反序列化
        T user = JSON.parseObject(userJson, userType);
        if(redisToken ==null || user ==null){
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            response.getWriter().write("还未登录");
            return null;
        }
        if(!redisToken.equals(token.substring(7))){
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getWriter().write("该账号已在另一台设备登录");
            return null;
        }
        return user;
    }

    /**
     * @Description: 验证跨服务token方法
     * @param token
     * @param response
     * @return boolean
     * @Author lhb
     * @CreateTime 2025/5/29 22:05
     */
    public boolean checkCrossToken(String token, HttpServletResponse response){
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        if (token == null || token.isEmpty()) return false;
        String userIdentity = JWTUtil.checkToken(token, authConfig.getJwtCrossServiceSecret());
        return !StrUtil.isBlank(userIdentity);
    }
}
